import { getDocsFilePath } from '@main/util/CommUtil'
import { getIpAddr } from '@main/util/IpUtil'
import { mutilWindowInstance } from '@main/window/MutilWindow'
import { app, ipcMain } from 'electron'

export function setupSystemHander(apiPort: number) {
  ipcMain.on('restart', () => {
    app.relaunch()
    app.exit()
  })
  ipcMain.handle('getApiPrefix', async () => {
    return `http://localhost:${apiPort}`
  })
  ipcMain.handle('getIpAddress', async () => {
    return getIpAddr()
  })
  ipcMain.handle('getServerPort', async () => {
    return apiPort
  })
  ipcMain.handle('newDocWin', async (_, flag: string, title: string, docFileName: string) => {
    const filePath = getDocsFilePath(docFileName)
    mutilWindowInstance.newWindowLoadFile({ title, flag, url: filePath })
  })
}
